home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / FLTK-1.0.6 / src / vsnprintf.c < prev   
Encoding:
C/C++ Source or Header  |  1999-03-29  |  3.1 KB  |  137 lines

  1. /*
  2.  * "$Id: vsnprintf.c,v 1.3.2.1 1999/03/29 17:39:29 carl Exp $"
  3.  *
  4.  * vsnprintf() function for the Fast Light Tool Kit (FLTK).
  5.  *
  6.  * Emulates this call on systems that lack it (pretty much everything
  7.  * except glibc systems).
  8.  *
  9.  * KNOWN BUGS:
  10.  *
  11.  * Field width & Precision is ignored for %%, %c, and %s.
  12.  *
  13.  * A malicious user who manages to create a %-fmt string that prints
  14.  * more than 99 characters can still overflow the temporary buffer.
  15.  * For instance %110f will overflow.
  16.  *
  17.  * Only handles formats that are both documented in the glibc man page
  18.  * for printf and also handled by your system's sprintf().
  19.  *
  20.  * Copyright 1998-1999 by Bill Spitzak and others.
  21.  *
  22.  * This library is free software; you can redistribute it and/or
  23.  * modify it under the terms of the GNU Library General Public
  24.  * License as published by the Free Software Foundation; either
  25.  * version 2 of the License, or (at your option) any later version.
  26.  *
  27.  * This library is distributed in the hope that it will be useful,
  28.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  29.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  30.  * Library General Public License for more details.
  31.  *
  32.  * You should have received a copy of the GNU Library General Public
  33.  * License along with this library; if not, write to the Free Software
  34.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  35.  * USA.
  36.  *
  37.  * Please report all bugs and problems to "fltk-bugs@easysw.com".
  38.  */
  39.  
  40. #include <stdio.h>
  41. #include <stdarg.h>
  42. #include <config.h>
  43.  
  44. #if !HAVE_VSNPRINTF
  45.  
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49.  
  50. int vsnprintf(char* str, size_t size, const char* fmt, va_list ap) {
  51.   const char* e = str+size-1;
  52.   char* p = str;
  53.   char copy[20];
  54.   char* copy_p;
  55.   char sprintf_out[100];
  56.  
  57.   while (*fmt && p < e) {
  58.     if (*fmt != '%') {
  59.       *p++ = *fmt++;
  60.     } else {
  61.       fmt++;
  62.       copy[0] = '%';
  63.       for (copy_p = copy+1; copy_p < copy+19;) {
  64.     switch ((*copy_p++ = *fmt++)) {
  65.     case 0:
  66.       fmt--; goto CONTINUE;
  67.     case '%':
  68.       *p++ = '%'; goto CONTINUE;
  69.     case 'c':
  70.       *p++ = va_arg(ap, int);
  71.       goto CONTINUE;
  72.     case 'd':
  73.     case 'i':
  74.     case 'o':
  75.     case 'u':
  76.     case 'x':
  77.     case 'X':
  78.       *copy_p = 0;
  79.       sprintf(sprintf_out, copy, va_arg(ap, int));
  80.       copy_p = sprintf_out;
  81.       goto DUP;
  82.     case 'e':
  83.     case 'E':
  84.     case 'f':
  85.     case 'g':
  86.       *copy_p = 0;
  87.       sprintf(sprintf_out, copy, va_arg(ap, double));
  88.       copy_p = sprintf_out;
  89.       goto DUP;
  90.     case 'p':
  91.       *copy_p = 0;
  92.       sprintf(sprintf_out, copy, va_arg(ap, void*));
  93.       copy_p = sprintf_out;
  94.       goto DUP;
  95.     case 'n':
  96.       *(va_arg(ap, int*)) = p-str;
  97.       goto CONTINUE;
  98.     case 's':
  99.       copy_p = va_arg(ap, char*);
  100.       if (!copy_p) copy_p = "NULL";
  101.     DUP:
  102.       while (*copy_p && p < e) *p++ = *copy_p++;
  103.       goto CONTINUE;
  104.     }
  105.       }
  106.     }
  107.   CONTINUE:;
  108.   }
  109.   *p = 0;
  110.   if (*fmt) return -1;
  111.   return p-str;
  112. }
  113.  
  114. #endif
  115.  
  116. #if !HAVE_SNPRINTF
  117.  
  118. int snprintf(char* str, size_t size, const char* fmt, ...) {
  119.   int ret;
  120.   va_list ap;
  121.   va_start(ap, fmt);
  122.   ret = vsnprintf(str, size, fmt, ap);
  123.   va_end(ap);
  124.   return ret;
  125. }
  126.  
  127. #ifdef __cplusplus
  128. }
  129. #endif
  130.  
  131. #endif
  132.  
  133. /*
  134.  * End of "$Id: vsnprintf.c,v 1.3.2.1 1999/03/29 17:39:29 carl Exp $".
  135.  */
  136.  
  137.